package edu.ts.crowdfunding.product.web;
import com.aliyun.oss.OSS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @Value("${aliyun.oss.bucket-name}")
    private String bucketName;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Autowired
    private OSS ossClient;
    @PostMapping("/upload")
    public String upload(MultipartFile file) throws IOException {
        // 生成唯一文件名
        String fileName = UUID.randomUUID() + "-" + file.getOriginalFilename();
        // 上传文件
        ossClient.putObject(bucketName, "tangshan/" + fileName, new ByteArrayInputStream(file.getBytes()));
        // 构建访问 URL（公共读模式）
        String fileUrl = "https://" + bucketName + "." + endpoint + "/tangshan/" + fileName;
        System.out.println(fileUrl);
        return fileUrl;
    }

}